// server/api/ai/chat-stream.post.ts
export default defineEventHandler(async (event) => {
    const body = await readBody(event)
    const { messages, model = 'qwen-plus' } = body
    
    const config = useRuntimeConfig()
    const apiKey = config.aiApiKey
    
    // 设置SSE响应头
    setResponseHeaders(event, {
      'Content-Type': 'text/event-stream',
      'Cache-Control': 'no-cache',
      'Connection': 'keep-alive',
    })
    
    try {
      const response = await $fetch('https://api.deepseek.com/v1/chat/completions', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${apiKey}`
        },
        body: JSON.stringify({
          model: model,
          messages: messages,
          stream: true,
          temperature: 0.7
        })
      })
      
      // 返回流式响应
      return response
    } catch (error) {
      return `data: ${JSON.stringify({ error: '流式响应失败' })}\n\n`
    }
  })